package org.andao.commons.taglib.ui.page;

import java.io.File;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;

import org.andao.commons.taglib.BaseTagSupport;
import org.andao.commons.template.FreeMarkerConfig;
import org.andao.core.dao.helper.Page;
import org.andao.core.utils.ExStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.support.RequestContext;


import freemarker.template.ObjectWrapper;
import freemarker.template.SimpleHash;
import freemarker.template.Template;

/**
 * 分页标签.
 * 
 * @author Marco.hu(hzg139@163.com)
 */
public class PageTag extends BaseTagSupport {

	private static final long serialVersionUID = -9218823737680856684L;

	private static Logger logger = LoggerFactory.getLogger(PageTag.class);

	transient private Page page; // 分页的实体对象
	
	private String goPageUrl;//页面跳转URL
	
	private String pageType = "sys";// 分页类型：sys-系统，other - 其他，uc - 用户中心
	
	private String targetType = "navTab";//分页类型 navTab|dialog|localArea   用来标记是navTab上的分页还是dialog上的分页
	
	private String localArea = "";//局部刷新的DIV ID，如果targetType选择localArea，这里必须赋值
	
	private String pageNumShown = "10";
	
	private String isShowPageSelector = "Y";//是否显示分页选择项,Y|N 是|否
				
	transient private Map<String,Object> condition;//查询条件，用来动态生成分页的查询条件
	
	private String style;//特别的样式
		
	public int doStartTag() throws JspException {
		if (page == null) {
			return super.doStartTag();
		}		
		try {
			Template template = null;
			Map<String,Object> root = getTemplateModelMap();
			PageInfo pi = PageInfo.getByPage(page);
			if("sys".equals(pageType)){
				template = FreeMarkerConfig.getDefaultTemplate("system"+File.separator+"page.ftl");
				root.put("goPageUrl", goPageUrl);
			}else if("other".equals(pageType)){
				template = FreeMarkerConfig.getDefaultTemplate("system"+File.separator+"page1.ftl");
				root.put("goPageUrl", goPageUrl);
				root.put("baseUrl", getBaseUrl());
			}else if("uc".equals(pageType)){
				template = FreeMarkerConfig.getDefaultTemplate("system"+File.separator+"page2.ftl");
				root.put("goPageUrl", goPageUrl);
				root.put("baseUrl", getBaseUrl());
			}
		
			root.put("style", ExStringUtils.isNotBlank(style) ? style: "");			
			root.put("targetType", targetType);			
			root.put("localArea", localArea);
			root.put("pageNumShown", pageNumShown);
			root.put("isShowPageSelector", isShowPageSelector);
			if(null != condition){
				SimpleHash hashMap = new SimpleHash(ObjectWrapper.DEFAULT_WRAPPER);
				Iterator it = condition.entrySet().iterator();    
				   while(it.hasNext()){    
				   Map.Entry entry = (Map.Entry)   it.next();    
				   hashMap.put(entry.getKey().toString(),entry.getValue());    
				 }   
				 root.put("condition", hashMap);   
			}
			root.put("pageInfo", pi);	
			setI18n(root);
			template.process(root, super.pageContext.getOut());
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return super.doStartTag();
	}

	private void setI18n(Map<String,Object> root){
		try{
			RequestContext ctx = new RequestContext(getrequest());
			root.put("pageInfo_allfind", ctx.getMessage("ALL_FIND"));
			root.put("pageInfo_records",ctx.getMessage("RECOURD_AS"));
			root.put("pageInfo_page", ctx.getMessage("PAGE"));
			root.put("pageInfo_allcheck", ctx.getMessage("ALL_CHECK"));
			root.put("pageInfo_passed", ctx.getMessage("CHECKED"));
			root.put("pageInfo_unpassed", ctx.getMessage("UNCHECKED"));
			root.put("pageInfo_eachpage", ctx.getMessage("EACH_SHOW"));
			root.put("pageInfo_item", ctx.getMessage("ITEM"));
		}catch(Exception e){
			logger.warn("加载国际化资源失败:{}",e.getMessage());
		}
	
	}
	
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getGoPageUrl() {
		return goPageUrl;
	}

	public void setGoPageUrl(String goPageUrl) {
		this.goPageUrl = goPageUrl;
	}

	public String getPageType() {
		return pageType;
	}

	public void setPageType(String pageType) {
		this.pageType = pageType;
	}

	public Map getCondition() {
		return condition;
	}

	public void setCondition(Map<String,Object> condition) {
		this.condition = condition;
	}

	public String getTargetType() {
		return targetType;
	}

	public void setTargetType(String targetType) {
		this.targetType = targetType;
	}

	public String getPageNumShown() {
		return pageNumShown;
	}

	public void setPageNumShown(String pageNumShown) {
		this.pageNumShown = pageNumShown;
	}

	/**
	 * @return the localArea
	 */
	public String getLocalArea() {
		return localArea;
	}

	/**
	 * @param localArea the localArea to set
	 */
	public void setLocalArea(String localArea) {
		this.localArea = localArea;
	}

	/**
	 * @return the isShowPageSelector
	 */
	public String getIsShowPageSelector() {
		return isShowPageSelector;
	}

	/**
	 * @param isShowPageSelector the isShowPageSelector to set
	 */
	public void setIsShowPageSelector(String isShowPageSelector) {
		this.isShowPageSelector = isShowPageSelector;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	

}
